home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11527 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  44 lines

  1. Path: coranto.ucs.mun.ca!usenet
  2. From: saustin@terra.nlnet.nf.ca (Steve Austin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q]Dereference within class - Possible?
  5. Date: Thu, 14 Mar 1996 23:43:42 GMT
  6. Organization: Kickham Productions
  7. Message-ID: <4iaav3$k53@coranto.ucs.mun.ca>
  8. References: <Do566t.F1H.0.queen@torfree.net>
  9. NNTP-Posting-Host: n104h130.nlnet.nf.ca
  10. X-Newsreader: Forte Agent .99b.112
  11.  
  12. bh332@freenet.toronto.on.ca (Karim Ladha) wrote:
  13. >In normal C style, one can code the following example(C++ compiler);
  14. >
  15. >int main() {
  16. >  int x   = 1;
  17. >  int &x1 = x;
  18. >  ...
  19. >}
  20. >
  21. >Is it also possible to use the same syntax within a class without intializing
  22. >the referencer? Any information will be appreciated...
  23. >
  24.  
  25. Karim, I think you're asking how to use references as class data
  26. members, is that right?  If so, then yes, you can use similar syntax,
  27. but you *have* to use an initializer list, like so...
  28.  
  29. class Example {
  30.  public:
  31.    Example(int&);
  32.  private:
  33.     int& x1;
  34. };
  35. Example::Example(int& x) : x1(x) {}
  36.  
  37. The initializer list has to be used to initialize reference and const
  38. members; it can't be done explicitly in the constructor body, because
  39. they have to be initialized before the constructor starts up.
  40.  
  41. Steve
  42.  
  43.  
  44.